package com.kwzjx.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.kwzjx.common.core.utils.DateUtils;
import com.kwzjx.common.security.utils.SecurityUtils;
import com.kwzjx.product.constant.ProductConstant;
import com.kwzjx.product.domain.MallProductSkuInfo;
import com.kwzjx.product.domain.req.MallProductInfoForm;
import com.kwzjx.product.service.IMallProductSkuInfoService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kwzjx.product.mapper.MallProductInfoMapper;
import com.kwzjx.product.domain.MallProductInfo;
import com.kwzjx.product.service.IMallProductInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品信息Service业务层处理
 *
 * @author kwzjx
 * @date 2024-05-31
 */
@Service
@Log4j2
public class MallProductInfoServiceImpl implements IMallProductInfoService
{
    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;

    @Autowired
    private IMallProductSkuInfoService skuInfoService;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public MallProductInfo selectMallProductInfoById(Long id)
    {
        return mallProductInfoMapper.selectMallProductInfoById(id);
    }

    /**
     * 查询商品信息列表
     *
     * @param mallProductInfo 商品信息
     * @return 商品信息
     */
    @Override
    public List<MallProductInfo> selectMallProductInfoList(MallProductInfo mallProductInfo)
    {
        return mallProductInfoMapper.selectMallProductInfoList(mallProductInfo);
    }

    /**
     * 新增商品信息
     *
     * @param mallProductInfo 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMallProductInfo(MallProductInfoForm mallProductInfo)
    {
        Result result = getResult(mallProductInfo);

        int i = mallProductInfoMapper.insertMallProductInfo(mallProductInfo);

        packageDataSaveOrUpdate(mallProductInfo, i, result.userid, result.userid1);

        return i;
    }

    /**
     * 修改商品信息
     *
     * @param mallProductInfo 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMallProductInfo(MallProductInfoForm mallProductInfo) {

        String step = mallProductInfo.getStep();
        log.info("入参的步骤是：{}",step);
        switch (step){
            case ProductConstant.STEP_ONE:
                Long id = mallProductInfo.getId();
                mallProductInfo.setSkuInfoList(null);
                skuInfoService.deleteMallProductSkuInfoProductById(id);
                break;
            case ProductConstant.STEP_TWO:
                mallProductInfo.setProductDesc("");
                break;
            default:
                break;
        }


        Result result = getResult(mallProductInfo);

        int i = mallProductInfoMapper.updateMallProductInfo(mallProductInfo);

        packageDataSaveOrUpdate(mallProductInfo, i, result.userid, result.userid1);

        return i;
    }

    @NotNull
    private static Result getResult(MallProductInfoForm mallProductInfo) {
        Long userid = SecurityUtils.getLoginUser().getUserid();
        mallProductInfo.setCreateBy(String.valueOf(userid));
        Long userid1 = SecurityUtils.getLoginUser().getUserid();
        mallProductInfo.setUpdateBy(String.valueOf(userid1));

        mallProductInfo.setUpdateTime(DateUtils.getNowDate());
        mallProductInfo.setCreateTime(DateUtils.getNowDate());
        Result result = new Result(userid, userid1);
        return result;
    }

    private static class Result {
        public final Long userid;
        public final Long userid1;

        public Result(Long userid, Long userid1) {
            this.userid = userid;
            this.userid1 = userid1;
        }
    }

    private void packageDataSaveOrUpdate(MallProductInfoForm mallProductInfo, int i, Long userid, Long userid1) {
        List<MallProductSkuInfo> skuInfoList = mallProductInfo.getSkuInfoList();
        if(i >0 && CollectionUtils.isNotEmpty(skuInfoList)) {
            Long id = mallProductInfo.getId();
            Date createTime = new Date();
            List<MallProductSkuInfo> mallProductSkuInfoList = new ArrayList<>();

            skuInfoService.deleteMallProductSkuInfoProductById(id);

            skuInfoList.stream().forEach(item -> {
                MallProductSkuInfo mallProductSkuInfo = new MallProductSkuInfo();
                BeanUtils.copyProperties(item, mallProductSkuInfo);
                mallProductSkuInfo.setProductId(id);

                mallProductSkuInfo.setCreateTime(createTime);
                mallProductSkuInfo.setUpdateTime(createTime);
                mallProductSkuInfo.setCreateBy(String.valueOf(userid));
                mallProductSkuInfo.setUpdateBy(String.valueOf(userid1));
                mallProductSkuInfoList.add(mallProductSkuInfo);
            });

            for (MallProductSkuInfo mallProductSkuInfo : mallProductSkuInfoList) {
                skuInfoService.insertMallProductSkuInfo(mallProductSkuInfo);
            }
        }
    }

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteMallProductInfoByIds(Long[] ids)
    {
        return mallProductInfoMapper.deleteMallProductInfoByIds(ids);
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteMallProductInfoById(Long id)
    {
        return mallProductInfoMapper.deleteMallProductInfoById(id);
    }

    @Override
    public MallProductInfoForm selectMallProductInfoAndSkuInfoById(Long id) {

        return mallProductInfoMapper.selectMallProductInfoAndSkuInfoById(id);
    }
}
